Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Redirect the browser when the output has already been sent

Total Hit ( 3567)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


If from an ASP page you try to use Response.Redirect after having written anything to the browser, you'll get an error saying that the page's headers have already been written. To avoid this you should put the code that checks if the browser needs to be redirected to another page at the very beginning of the page, before any HTML code. A better approach is to turn on page buffering (which is on by default in IIS 5).

There are cases, however, when you find out you need to redirect the user to another page after sending partial output to the browser. For example, this happens if your ASP code performs a time consuming task, and you want to display a message or an animated image that shows the progress status.

How to redirect to another page if the browser has already received some html? The trick is to send to the browser a client side script that do the action. As soon as the browser receives the script, it will be processed performing the redirection. The client side script can load another page by setting the location.href property. Here's the ASP function that you can use instead of Response.Redirect:

Click here to copy the following block
Sub RedirectTo(strURL)
  Response.Write "<SCRIPT LANGUAGE=""JavaScript"" TYPE=""text/javascript"">" _
    & "location.href = '" & strURL & "';" & "</SCRIPT>"
End Sub

To show a complete example, consider the following script:
&lt;%
Sub RedirectTo(byval strURL)
  Response.Write "&lt;SCRIPT LANGUAGE=""JavaScript"" TYPE=""text/javascript" _
    ">" & "location.href = '" & strURL & "';" & "&lt;/SCRIPT>"
End Sub

Response.Write "&lt;H1>The server is processing your order. Please " _
  & "wait...&lt;/H1>"

' write here the code that actually performs the time consuming task
' ...
' ...

' at the end, redirect the browser to the confirmation page
RedirectTo "confirm.htm"
%>



Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.